package com.jlnk.stockdemo.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.jlnk.stockdemo.R;
import com.jlnk.stockdemo.bean.StockDetailResponse;
import com.jlnk.stockdemo.viewmodel.StockViewModel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StockActivity extends AppCompatActivity {
    private static final String TAG = "StockActivity";
    private StockViewModel stockViewModel;
    private List<StockDetailResponse.Data.StockDetail> stockDetailList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock);
        initView();
    }

    private void initView() {
        TextView tv_name = findViewById(R.id.tv_name);
        TextView tv_symbol = findViewById(R.id.tv_symbol);
        TextView tv_open = findViewById(R.id.tv_open);
        TextView tv_high = findViewById(R.id.tv_high);
        TextView tv_close = findViewById(R.id.tv_close);
        TextView tv_low = findViewById(R.id.tv_low);
        TextView tv_pre_close = findViewById(R.id.tv_pre_close);
        TextView tv_change = findViewById(R.id.tv_change);
        TextView tv_vol = findViewById(R.id.tv_vol);
        TextView tv_pct_chg = findViewById(R.id.tv_pct_chg);
        TextView tv_amount = findViewById(R.id.tv_amount);
        LineChart lineChart = findViewById(R.id.lineChart);
        tv_name.setText(getIntent().getStringExtra("name"));
        tv_symbol.setText("(" + getIntent().getStringExtra("symbol") + ")");
        String ts_code = getIntent().getStringExtra("ts_code");
        stockViewModel = new ViewModelProvider(this).get(StockViewModel.class);
        stockViewModel.getStockDetailLiveData().observe(this, stockDetails -> {
            List<Entry> entries = new ArrayList<>();
            Collections.reverse(stockDetails);
            Log.d(TAG, "stockDetails.size: " + stockDetails.size());
            for (int i = 0; i < stockDetails.size(); i++) {
                entries.add(new Entry(i, Float.parseFloat(stockDetails.get(i).getHigh()),
                        stockDetails.get(i).getTrade_date()));
            }
            LineDataSet lineDataSet = new LineDataSet(entries, "Stock Price");
            lineDataSet.setDrawCircles(false);
            lineDataSet.setDrawValues(true);
            LineData lineData = new LineData(lineDataSet);
            lineChart.setData(lineData);
            // 设置X轴
            XAxis xAxis = lineChart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setGranularity(1f);
            xAxis.setValueFormatter(new IndexAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value) {
                    int index = (int)value;
                    return stockDetails.get(index).getTrade_date();
                }
            });
            lineChart.invalidate(); // 刷新图表
            tv_open.setText(stockDetails.get(stockDetails.size()-1).getOpen());
            tv_high.setText(stockDetails.get(stockDetails.size()-1).getHigh());
            tv_close.setText(stockDetails.get(stockDetails.size()-1).getClose());
            tv_low.setText(stockDetails.get(stockDetails.size()-1).getLow());
            tv_pre_close.setText(stockDetails.get(stockDetails.size()-1).getPre_close());
            tv_change.setText(stockDetails.get(stockDetails.size()-1).getChange());
            tv_vol.setText(stockDetails.get(stockDetails.size()-1).getVol());
            tv_pct_chg.setText(stockDetails.get(stockDetails.size()-1).getPct_chg());
            tv_amount.setText(stockDetails.get(stockDetails.size()-1).getAmount());
        });
        stockViewModel.fetchStockDetail(ts_code);

    }
}
